//
//  AppMonitor.m
//  haolaDoc
//
//  Created by chen on 2017/5/16.
//  Copyright © 2017年 deyi. All rights reserved.
//

#import "AppMonitor.h"
#import "SVProgressHUD.h"
#import "AFHTTPRequestOperation.h"
#import "LoginViewController.h"
@implementation AppMonitor
+(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = NO;
    NSURL *url1 = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: nil];
    if (response == nil) {
        isExistenceNetwork=NO;
    }
    else{
        isExistenceNetwork=YES;
    }
    return isExistenceNetwork;
}
/**
 *  对话框
 *
 *  @param owner         发起者
 *  @param status        内容
 *  @param actionOKblock 点击按钮事件
 */

+(void)showResultWithOwner:(UIViewController *)owner withStatus:(NSString *)status actionOk:(dispatch_block_t)actionOKblock{
    //初始化提示框；
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:status preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
        if (actionOKblock) {
            actionOKblock();
        }
    }]];
    //弹出提示框；
    [owner presentViewController:alert animated:true completion:nil];
}
/**
 *  正则表达式匹配电话号码状态
 *
 *  @param mobile 手机号码
 *
 *  @return 是否为手机号码
 */

+(BOOL *)isMobileNumber:(NSString *)mobile{
    if (mobile.length < 11)
    {
        return false;
    }else{
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return YES;
        }else{
            return false;
        }
    }
    return false;
}

/**
 *  封装的AFN的异步请求方法适用于大部分的与服务器的请求交互，本方法仅适用于url带参数的请求方式，后期会逐步加上使用param方法请求参数的形式。
 *
 *  @param urlStr       请求的连接，注意：只需要填写接口的名称和参数，不需要加BASE_URL
 *  @param successBlock 成功的block，可以通过传入调用类中字典的方式获取AFN请求的值
 *  @param failBlock    失败的block，结果同上
 */
+(void)parseDicByUrl:(NSString *)urlStr withSuccessBlock:(void (^)(NSData * successDic))successBlock andFailBlock:(void (^)(NSData * failDic))failBlock{
    NSString * path = [NSString stringWithFormat:BASE_URL@"%@",urlStr];
    NSURL * url = [NSURL URLWithString:path];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* datas=[html dataUsingEncoding:NSUTF8StringEncoding];
        successBlock(datas);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"block内部产生了错误%@,当前Url为：%@",error,url);
        NSString *html = operation.responseString;
        NSData* datas=[html dataUsingEncoding:NSUTF8StringEncoding];
        failBlock(datas);
    }];
    
    [operation start];
}


+(void)getScrollViewPictures{
    [AppMonitor parseDicByUrl:@"/scrollImg/getOndateImg?client=doc" withSuccessBlock:^(NSData *successDic) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithObject:successDic forKey:@"data"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"loadScrollImages" object:nil userInfo:dicInfo];
    } andFailBlock:^(NSData *failDic) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithObject:failDic forKey:@"data"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"loadScrollImages" object:nil userInfo:dicInfo];
    }];
}
/**
 *  设置避免nil数据造成的程序崩溃
 *
 *  @param nilStr 可能为nil的数据
 *
 *  @return 返回处理后的数据
 */
+(id)parseNilString:(id)nilStr{
    if ([nilStr isEqual:[NSNull null]]){
        return @"null";
    }
    else{
        return nilStr;
    }
}
/**
 *  同上
 *
 *  @param nilNum
 *
 *  @return
 */
+(id)parseNilNum:(id)nilNum{
    if ([nilNum isEqual:[NSNull null]]){
        return @0;
    }
    else{
        return nilNum;
    }
}
+(id)parseParam:(id)param{
    if ([param isEqual:[NSNull null]]){
        return @"null";
    }
    else if ([param isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@",param];
    }
    else{
        return param;
    }
}
+(BOOL)checkIsLogin{
    NSString *isOnline = [[NSUserDefaults standardUserDefaults] objectForKey:@"isOnline"];
    if (isOnline == nil || isOnline == NULL) {
        //未登录时的情况,点击头像登录;
        return NO;
    }else{
        return YES;
    }
    
}
+(void)jumpToLoginWithUIController:(UINavigationController *)naviagtionController{

    LoginViewController *loginView=[naviagtionController.storyboard instantiateViewControllerWithIdentifier:@"loginViewController"];
    [naviagtionController pushViewController:loginView animated:YES];
}
/**
 *  获取主题颜色
 *
 *  @return <#return value description#>
 */
+(UIColor *)getThemeColor{
    //UIColor *color=[UIColor colorWithRed:67.0f/255 green:196.0f/255 blue:171.0f/255 alpha:1.0f];
    //UIColor *color=[UIColor colorWithRed:30.0f/255 green:144.0f/255 blue:255.0f/255 alpha:1.0f];
    UIColor *color=[UIColor colorWithRed:0 green:0.57f blue:0.89f alpha:1.0f];
    return color;
}

@end
